'use strict';

let users=[
    {
        id:1,
        username:'admin',
        password:'113',
        nickname:'超级管理员',
        avatar:'这是一很帅的男人的头像'
    },
    {
        id:2,
        username:'user01',
        password:'113',
        nickname:'路人1',
        avatar:'这是一很帅的男人的头像'
    },
    {
        id:3,
        username:'user02',
        password:'113',
        nickname:'路人2',
        avatar:'这是一很帅的男人的头像'
    },
]

let fn_index=async (ctx,next)=>{
    ctx.body=[
        {
            id:1,
            username:'admin',
            nickname:'超级管理员'
        },
        {
            id:2,
            username:'user01',
            nickname:'我是好男人'
        },
        {
            id:3,
            username:'user02',
            nickname:'我是帅男人'
        },
    ]
}

let fn_create=async (ctx,next)=>{
    // 获取post过来的json参数
    let username=ctx.request.body.username;
    let password=ctx.request.body.password;

    // 利用数组的过滤函数，找到用户名和密码相符的用户，返回一个全新的数组，数组中是用户名和密码通过验证的用户
    let list=users.filter(item=>{
        return item.username===username && item.password===password;
    })
    if(list.length>0){
        ctx.body={
            code:1000,
            data:list[0],
            msg:'登录成功'
        }
    }else{
        ctx.body={
            code:400,
            data:'',
            msg:'用户名或者密码错误，请确认后重试'
        }
    }
    
}

module.exports={
    'post /user':fn_create,
    'get /user':fn_index,
    'post /login':fn_create
}